<?php
include_once(I('server.DOCUMENT_ROOT') . '/XgPush/' . 'function.php');
include_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'function.php');

function PushUid($uid,$title='',$content='',$trans=array()){
	$user=M('UserLoginInfo')->where(array('reporter_id'=>$uid,'valid_status'=>1))->find();
	if($user){
		if(strtoupper($user['clienttype'])=='ANDROID'){
			return PushSingleDevice($user['channelid'],$title,$content,$trans);
		}else if(strtoupper($user['clienttype'])=='IOS'){
			return  PushAPN($user['channelid'],$title,$content,$trans);
		}else{
			return array('ret_code'=>'-100','mess'=>'未知的设备');
		}
	}else{
			return array('ret_code'=>'-100','mess'=>'用户未登录');
	}
}

function PushUidList($uidlist,$title='',$content='',$trans=array()){
	$user=M('UserLoginInfo')->where(array('reporter_id'=>array('in',$uidlist),'valid_status'=>1))->select();
	if($user){
		$userlist=array(
			'ANDROID'=>array(),
			'IOS'=>array(),
		);
		foreach($user as $v){
			if(strtoupper($v['clienttype'])=='ANDROID'){
				$userlist['ANDROID'][]=$v['channelid'];
			}else if(strtoupper($v['clienttype'])=='IOS'){
			

				$userlist['IOS'][]=$v['channelid'];
			}
		}
	
		$rep['ANDROID']= PushDeviceListMultipleNotification($userlist['ANDROID'],$title,$content,$trans);
		$rep['IOS']= PushAPNListMultipleNotificationIOS($userlist['IOS'],$title,$content,$trans);

		
		/*foreach($userlist as $clienttype=>$accountList){
				print_r($userlist);

			if(strtoupper($clienttype)=='ANDROID'){
				$rep['ANDROID']= PushDeviceListMultipleNotification($accountList,$title,$content,$trans);
			}else if(strtoupper($clienttype)=='IOS'){
				print_r($accountList);
				$rep['IOS']=PushDeviceListMultipleNotificationIOS($accountList,$title,$content,$trans);
			}else{
				return array('ret_code'=>'-100','mess'=>'未知的设备');
			}*/
			return $rep;
		
	}else{
		return array('ret_code'=>'-100','mess'=>'用户未登录');
	}
}

function PushChannel($chid,$title='',$content='',$trans=array()){
	$uid=M('UserInfo')->where(array('userchannel'=>$chid))->select();
	foreach($uid as $v){
		$uidlist[]=$v['uid'];
	}
	return PushUidList($uidlist,$title,$content,$trans);
}